<?php
require(dirname(__FILE__)."/"."../inc/common.inc.php");
admin(15);

//将HTML表格的每行每列转为数组
function get_td_array($table) {
	$table = preg_replace("'<table[^>]*?>'si","",$table);
	/*$table = preg_replace("'<tr[^>]*?>'si","",$table);*/
	$table = preg_replace("'<tr[^>]*?>'si","<tr>",$table);
	$table = preg_replace("'<td[^>]*?>'si","",$table);
	//$table = str_replace("</tr>","{tr}",$table);
	$table = str_replace("<tr>","{tr}",$table);	
	$table = str_replace("</td>","{td}",$table);
	//去掉 HTML 标记 
	/*$table = preg_replace("'<[/!]*?[^<>]*?>'si","",$table);*/	
	//去掉空白字符 
	$table = preg_replace("'([rn])[s]+'","",$table);
	$table = str_replace(" ","",$table);
	$table = str_replace(" ","",$table);
	$table = explode('{tr}', $table);
	//array_pop($table);// 这个表格没有/tr标签 无需 弹出数组最后一个单元 
	foreach ($table as $key=>$tr) {
		$td = explode('{td}', $tr);
		array_pop($td);
		$td_array[] = $td;
	}
	return $td_array;
}

/**
 * php截取指定两个字符之间字符串方式二
 * @param string $str    需要截取的字符串
 * @param string $start  开始字符串
 * @param string $end    结束字符串
 * @return string
 */
function get_between($str, $start, $end) {
    $substr = substr($str, strlen($start)+strpos($str, $start),(strlen($str) - strpos($str, $end))*(-1));
    return $substr;
}
//把数字标红的替换并返回是第几个数字为红
function chen_chr($str){
	$return=array();
	$return['red']=get_between($str,"<fontcolor='#FF0000'>",'</font>');
	$return['str']=preg_replace("'<[/!]*?[^<>]*?>'si","",$str);	
	$return['key'] = array_search($return['red'],str_split($return['str']));	 
 	return $return;	
}



if($action=='post'){
	$url="https://zst.cjcp.com.cn/cjw3d/view/3d_kaijiang-3-".($number?$number:50).".html";
	$html=file_get_contents($url);

	//这个数据没有tr结束标签
	$table=get_between($html,'<tbody id="pagedata">','<!-- END -->');
	$table=trim($table);

	$tablearr=get_td_array($table);

	foreach($tablearr as $key=>$rs){
		if($rs[0]!=''){		
			$rs[5]=(chen_chr($rs[5]));
			$rs[6]=(chen_chr($rs[6]));
			$rs[7]=(chen_chr($rs[7]));		
			$rs[9]=(chen_chr($rs[9]));
			$rs[10]=(chen_chr($rs[10]));
			$rs[11]=(chen_chr($rs[11]));
			if(!$db->get_one("select id from  `{$pre}3d_map` where  `date`='{$rs[1]}'  ")){
					$db->query("INSERT INTO `{$pre}3d_map` set `sn`='{$rs[0]}', `date`='{$rs[1]}', `testnum`='{$rs[2]}', `lotnum`='{$rs[3]}', `total`='{$rs[4]}', `t_hund`='{$rs[5]['str']}', `t_hund_red`='{$rs[5]['red']}', `t_hund_key`='{$rs[5]['key']}', `t_decade`='{$rs[6]['str']}', `t_decade_red`='{$rs[6]['red']}', `t_decade_key`='{$rs[6]['key']}', `t_bit`='{$rs[7]['str']}', `t_bit_red`='{$rs[7]['red']}', `t_bit_key`='{$rs[7]['key']}', `l_hund`='{$rs[9]['str']}', `l_hund_red`='{$rs[9]['red']}', `l_hund_key`='{$rs[9]['key']}', `l_decade`='{$rs[10]['str']}', `l_decade_red`='{$rs[10]['red']}', `l_decade_key`='{$rs[10]['key']}', `l_bit`='{$rs[11]['str']}', `l_bit_red`='{$rs[11]['red']}', `l_bit_key`='{$rs[11]['key']}' ");
			}
			
			
		}	
	}
	logo('抓取数据');
	refreshto('?','抓取成功!',2);

}
?>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<meta name="renderer" content="webkit|ie-comp|ie-stand">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
<!--[if lt IE 9]>
<script type="text/javascript" src="js/html5.js"></script>
<script type="text/javascript" src="js/respond.min.js"></script>
<script type="text/javascript" src="js/PIE_IE678.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="css/H-ui.css"/>
<link type="text/css" rel="stylesheet" href="css/H-ui.admin.css"/>
<link type="text/css" rel="stylesheet" href="font/font-awesome.min.css"/>
<!--[if IE 7]>
<link href="font/font-awesome-ie7.min.css" rel="stylesheet" type="text/css" />
<![endif]-->
<title>开奖列表</title>
</head>
<body>
<nav class="Hui-breadcrumb"> <?=get_category(15)?> <a class="btn btn-success radius r mr-20" style="line-height:1.6em;margin-top:3px" href="javascript:location.href='?';" title="刷新" ><i class="icon-refresh"></i></a></nav>
<div class="pd-20">
  <div class="">
  <script type="text/javascript">
var iszq=0;
function checas(){
	if(iszq==1){
		alert('系统处理中');
		return false;
	}
	iszq=1;
}
  </script>
    <form class="Huiform" method="post" onsubmit="return checas();">
    <input type="hidden" name="action" value="post">
    抓取最近多少期数据
      <input type="number" placeholder="输入抓取最近多少期" autocomplete="off" value="50" name="number"  class="input-text">
     
      <button type="submit" class="btn btn-success" id="12" name="12" value="12" ><i class="icon-plus"></i> 抓取</button>

     
    </form>
</div>


</div>

<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/Validform_v5.3.2_min.js"></script> 
<script type="text/javascript" src="layer/layer.min.js"></script>
<script type="text/javascript" src="js/H-ui.js"></script>
<script type="text/javascript" src="js/H-ui.admin.js"></script>
<script type="text/javascript">
$(".Huiform").Validform(); 
</script>
</body>
</html>